package com.sql.azarias.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.sql.azarias.R;
import com.sql.azarias.entity.ProjectEntity;
import com.sql.azarias.entity.UserInfoEntity;
import com.sql.azarias.http.EntityListRequest;
import com.sql.azarias.http.EntityRequest;
import com.sql.azarias.http.Result;
import com.sql.azarias.http.SimpleHttpListener;
import com.sql.azarias.http.StringRequest;
import com.sql.azarias.views.ToastView;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;

import java.util.List;

public class MainActivity extends BaseActivity {

    private static final int NOHTTP_USER_INFO = 1;
    private Context context = MainActivity.this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_request).setOnClickListener(requestClick);
        findViewById(R.id.btn_request_entity).setOnClickListener(requestClick);
        findViewById(R.id.btn_request_entity_list).setOnClickListener(requestClick);
    }

    /**
     * 点击请求按钮的时候请求。
     */
    private View.OnClickListener requestClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.btn_request: {
                    requestString();
                   // startActivity(new Intent( context, LiveActivity.class));
                  //  startActivity(new Intent(context,PlayerActivity.class));
                    break;
                }
                case R.id.btn_request_entity: {
//                    requestEntity();
//                    startActivity(new Intent(context,SampleActivity.class));
                    startActivity(new Intent(context,ScrollActivity.class));
                    break;
                }
                case R.id.btn_request_entity_list: {
                    requestEntityList();
                    break;
                }
            }
        }
    };

    /**
     * 请求String。
     */
    private void requestEntityList() {
        String url = "http://api.nohttp.net/restJsonArray";
//        String url = "http://192.168.1.226/HttpAPI/restJsonArray";
        EntityListRequest<ProjectEntity> entityRequest = new EntityListRequest<>(url, RequestMethod.POST, ProjectEntity.class);
        entityRequest.add("name", "yanzhenjie");
        entityRequest.add("pwd", 123);

        request(context,NOHTTP_USER_INFO, entityRequest, new SimpleHttpListener<List<ProjectEntity>>() {
            @Override
            public void onSucceed(int what, Result<List<ProjectEntity>> t) {

                if (t.isSucceed()) { // 我们自己的业务状态成功。
                    List<ProjectEntity> projectEntityList = t.getResult();
                    for (ProjectEntity projectEntity : projectEntityList) {
                        Logger.i("项目名字：" + projectEntity.getName());
                        Logger.i("项目说明：" + projectEntity.getComment());
                        Logger.d("-------------------");
                    }
                } else {
                    ToastView.getInstance().show(t.getError(),context);
                }
            }
        });
    }

    /**
     * 请求String。
     */
    private void requestEntity() {
        String url = "http://api.nohttp.net/restJsonObject";
        EntityRequest<UserInfoEntity> entityRequest = new EntityRequest<>(url, RequestMethod.GET, UserInfoEntity.class);
        entityRequest.add("name", "yanzhenjie");
        entityRequest.add("pwd", 123);

        request(context,NOHTTP_USER_INFO, entityRequest, new SimpleHttpListener<UserInfoEntity>() {
            @Override
            public void onSucceed(int what, Result<UserInfoEntity> t) {// Http层的成功。
                if (t.isSucceed()) { // 我们自己的业务状态成功。
                    UserInfoEntity userInfoEntity = t.getResult();
                    Logger.i("名字：" + userInfoEntity.getName());
                    Logger.i("博客：" + userInfoEntity.getBlog());
                    Logger.i("网站：" + userInfoEntity.getUrl());


                    List<ProjectEntity> projectEntityList = userInfoEntity.getProjectList();
                    for (ProjectEntity projectEntity : projectEntityList) {
                        Logger.i("项目名字：" + projectEntity.getName());
                        Logger.i("项目说明：" + projectEntity.getComment());
                        Logger.d("-------------------");
                    }
                } else {
                    ToastView.getInstance().show(t.getError(),context);
                }
            }
        });
    }

    /**
     * 请求String。
     */
    private void requestString() {
        String url = "http://live.bilibili.com/AppIndex/home?";
        StringRequest request = new StringRequest(url, RequestMethod.GET);
        request.add("_device", "android");
        request.add("_hwid", "51e96f5f2f54d5f9");
        request.add("_ulv", "10000");
        request.add("access_key", "563d6046f06289cbdcb472601ce5a761&appkey");
        request.add("appkey", "c1b107428d337928&");
        request.add("build", "410000");
        request.add("platform", "android");
        request.add("scale", "xxhdpi");
        request.add("sign", "fbdcfe141853f7e2c84c4d401f6a8758");
        request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
        request(context,NOHTTP_USER_INFO, request, httpListener);
    }

    /**
     * 处理String结果。
     *
     * @param result
     */
    private void result(Result<String> result) {
        if (result.isSucceed()) {
            String jsonString = result.getResult();

            Logger.i("请求成功：" + jsonString);
        } else {
            ToastView.getInstance().show(result.getError(),context);
        }
    }

    private SimpleHttpListener<String> httpListener = new SimpleHttpListener<String>() {
        @Override
        public void onSucceed(int what, Result<String> t) {
            switch (what) {
                case NOHTTP_USER_INFO: {
                    result(t);
                    break;
                }
            }
        }
    };

}
